import { RouterView } from 'vue-router';
import { defineComponent, onErrorCaptured, ref, onBeforeMount } from 'vue';
import ErrorPage from './phoneOa/pages/home/error/index';
import { useHomeWorksProvide } from '@/domain/correction/hooks/homeWork';
import { useUserInfoProvide } from '@/domain/correction/hooks/login';
import { usePrepareProvide } from '@/domain/correction/hooks/prepare';
import { useCurrectStdProvide } from '@/domain/correction/hooks/currectStd';

export default defineComponent({
  name: 'App',
  setup() {
    const errorHappened = ref<boolean>(false);
    const errorMessage = ref<string>('');
    // 初始化菜单
    const loginStore = useUserInfoProvide();
    /**备课hooks */
    usePrepareProvide();
    useHomeWorksProvide(); //作业列表hooks
    useCurrectStdProvide(); //批改作业

    onBeforeMount(async () => {
      // menuStore.initMenuData(menuList);
      await loginStore.init();
      // if (loginStore.state.isLoginStatus) {
      // }
    });
    onErrorCaptured((err, instance, info) => {
      errorMessage.value = err.message;
      errorHappened.value = true;
      return false;
    });
    function renderNormalPage() {
      return <RouterView></RouterView>;
    }

    function renderErrorPage() {
      return <ErrorPage message={errorMessage.value} />;
    }

    return () => <>{errorHappened.value ? renderErrorPage() : renderNormalPage()}</>;
  }
});
